; fire20.asm
; Fire effect (Vic-20)

	.macpack cbm

	VIDEO_BASE	= $1C00
	CHAR_BASE	= $1000
	COLOR_BASE  = $9400
	
	CHAR_GEN = $8000
	
	TEXT_ROW = 8
	
	VIDEO_PAGES = 4

	VIC_BASE = $9000

	PAL_COLS = 28
	PAL_ROWS = 35

	COLOR_BLACK = 0
	COLOR_WHITE = 1
	COLOR_RED = 2
	COLOR_CYAN = 3
	COLOR_PURPLE = 4
	COLOR_GREEN = 5
	COLOR_BLUE = 6
	COLOR_YELLOW = 7
	
	COLOR_ORANGE = 8
	COLOR_LIGHT_ORANGE = 9
	COLOR_LIGHT_RED = 10
	COLOR_LIGHT_CYAN = 11
	COLOR_LIGHT_PURPLE = 12
	COLOR_LIGHT_GREEN = 13
	COLOR_LIGHT_BLUE = 14
	COLOR_LIGHT_YELLOW = 15
	
	.segment "ZEROPAGE"
	
ptr:		.res 2
cnt:		.res 1
hlpA:		.res 1
hlpD:		.res 1		 

	.segment "BOOT"

; Startup Basic program "2011 SYS prolog"
			.word	basprg
basprg:		.word	@end
			.word	2011
			.byte	$9E
			.byte	<(init / 1000 .mod 10) + $30
			.byte	<(init / 100 .mod 10) + $30
			.byte	<(init / 10 .mod 10) + $30
			.byte	<(init / 1 .mod 10) + $30
			.byte	0
@end:		.word	0

			.res $01F3

	.segment "CODE"

init:		; clear screen & set-up char RAM
			ldx #0
			txa
@clrvideo:	sta VIDEO_BASE+$0000, x	
			sta VIDEO_BASE+$0100, x	
			sta VIDEO_BASE+$0200, x	
			sta VIDEO_BASE+$0300, x
			inx
			bne @clrvideo
@cploop:	lda table2+$0000, x
			sta CHAR_BASE+$0000, x
			lda table2+$0100, x
			sta CHAR_BASE+$0100, x
			inx
			bne @cploop
			; set-up colors
			lda #COLOR_ORANGE*16
			sta VIC_BASE+14
			lda #COLOR_RED+8
			sta VIC_BASE+15
			lda #COLOR_YELLOW+8
@clrcol:	sta COLOR_BASE+$0000, x	
			sta COLOR_BASE+$0100, x	
			sta COLOR_BASE+$0200, x	
			sta COLOR_BASE+$0300, x
			inx
			bne @clrcol
			; setup video mode (PAL)
			lda	#6				; horizontal line start
			sta	VIC_BASE+0
			lda #15				; first line
			sta VIC_BASE+1
			lda #PAL_COLS		; columns
			sta VIC_BASE+2
			lda VIC_BASE+3
			and #$80
			ora #PAL_ROWS*2		; rows & char height (single)
			sta VIC_BASE+3
			lda #$FC			; video & character address 
			sta VIC_BASE+5
			
			;
			; main loop
			;

@mloop:		; calc fire
			inc cnt
			ldx #PAL_COLS+1
@loop6:		lda CHAR_BASE, x
			adc cnt
			tay
			lda CHAR_BASE, y
			lsr
			lsr
			sta VIDEO_BASE+PAL_COLS*PAL_ROWS-1, x		; invisible line 26
			dex
			bne @loop6
			stx ptr
			lda #>VIDEO_BASE
			sta ptr+1
			lda #VIDEO_PAGES
			sta hlpD
@loop7:		clc
			ldy #PAL_COLS-1
			lda (ptr), y
			iny
			adc (ptr), y
			iny
			adc (ptr), y
			ldy #0
			adc (ptr), y
			sbc #3
			bcs @nxt7
			lda #0
@nxt7:		lsr
			lsr
			sta (ptr), y
			inc ptr
			bne @nxt8
			inc ptr+1
@nxt8:		dex
			bne @loop7
			dec hlpD
			bne @loop7

			jmp @mloop

;----------------------------------------------------------------------------

	.segment "RODATA"
	
table1:
	.incbin "firetab.bin"
	
table2:
	.incbin "firechars.bin"
	
